<template>
	<view class="weChatLogin">
		<template v-if="loginStatus == 'loading'">
			<global-loading :item="8"></global-loading>
		</template>
		<template v-if="loginStatus == 'select' && weixinCode">
			<view class="yes" @click="$_to('pages/loginOrRegister')">
				我有账号，我要关联绑定
			</view>
			<view class="no" @click="clickNoAccountButton">
				我没有账号，帮我生成新账号
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginStatus:"loading",
				weixinCode:""
			}
		},
		onLoad() {
			this.getLogin();
		},
		methods: {
			getLogin(){
				uni.login({
					provider:'weixin',
					success: (res) => {
						this.weixinCode = res.code;
						this.postWeChatLoginOne();
					}
				})
			},
			// 微信登录方法，第一次登录
			postWeChatLoginOne(){
				uni.request({
					url:this.$_url() + "/api/wechat-login/mini-program/oauth",
					method:"POST",
					header:this.$_fresnsHeader(),
					data:{
						"code":this.weixinCode,
					},
					success: (res) => {
						if(res.data.code == 0){
							this.$_to("my/userSelection?loginToken="+res.data.data.loginToken)
						}else{
							this.loginStatus = "select"
						}
					}
				})
			},
			// 没有账号按钮
			clickNoAccountButton(){
				const _this = this;
				uni.showLoading();
				uni.login({
					provider:'weixin',
					success: (res) => {
						_this.weixinCode = res.code;
						_this.$_request(
							"POST","/api/wechat-login/mini-program/oauth",
							{
								"code":_this.weixinCode,
								"autoRegister":true
							},
							(res_) => {
								_this.getLogin();
							}
						)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
.yes{
	width: 700rpx;
	background: $theme;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 25rpx 25rpx 0 25rpx;
	font-size: 32rpx;
	line-height: 32rpx;
	color: #FFF;
	border-radius: 10rpx;
	padding: 50rpx 0;
	flex-flow: column;
	view{
		color: #FFF;
	}
}
.no{
	width: 700rpx;
	background: $green;
	display: flex;
	align-items: center;
	justify-content: center;
	margin: 25rpx 25rpx 0 25rpx;
	font-size: 32rpx;
	line-height: 32rpx;
	color: #FFF;
	border-radius: 10rpx;
	padding: 50rpx 0;
}
</style>
